package util;

import java.util.Random;

public class yanghuiTriangle {
    public static void main(String[] args) {
        int[][] a = creatYangHuiTriangle(10);
        showYangHuiTriangle(a,0);
        showYangHuiTriangle(a,1);
        showYangHuiTriangle(a,2);
        showYangHuiTriangle(a,3);
    }
    // 生成随机一维数组，范围 [ 0 - 100)
    public static int[][] creatYangHuiTriangle(int Depth){
        Random r = new Random();
        int[][] YangHuiTriangle = new int[Depth][Depth];
        for (int i = 0; i < Depth; i++) {
            for (int j = 0; j < Depth; j++) {
                YangHuiTriangle[i][j] = r.nextInt(50);
            }
        }
        return YangHuiTriangle;
    }


    // 输出杨辉三角
    public static void showYangHuiTriangle(int[][] YangHuiTriangle){
        int row = YangHuiTriangle.length;
        int cloum = YangHuiTriangle[0].length;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(YangHuiTriangle[i][j]+"\t");
            }
            System.out.println();
        }
        System.out.println();
    }
    // 输出杨辉三角
/**
    %d  ：按整型数据的实际长度输出； <p>
    %md ：m为指定的输出宽度。位数小于m，左端补空格；大于m，实际位数输出; <p>
    %0md：数据的位数小于m，则左端补0；大于m，实际位数输出。 <p>
    上面的转换标志则是"+""-"或者“0”之间的一个:<p>
        “+” 右对齐，默认形式
        “-” 左对齐
        “0” 用0填充空出的位置
 * @Params 输出宽度width：
 * @param YangHuiTriangle 杨辉三角矩阵
 * @param width   输出宽度width：0：靠左输出，1叫窄输出，2较宽输出，3：靠右输出
 * @return 5
*/
    public static void showYangHuiTriangle(int[][] YangHuiTriangle,int width){
        int row = YangHuiTriangle.length;
        int cloum = YangHuiTriangle[0].length;
        if(width == 0){
            for (int i = 0; i < row; i++) {
                for (int j = 0; j <= i; j++) {
                    System.out.print(YangHuiTriangle[i][j]+"\t");
                }
                System.out.println();
            }
        }
        if(width == 1) {
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < row - i - 1; j++) {
                    System.out.printf("  ");
                }
                for (int j = 0; j <= i; j++) {
                    System.out.printf("%-4d", YangHuiTriangle[i][j]);

                }
                System.out.println();
            }
        }
        if(width == 2) {
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < row - i - 1; j++) {
                    System.out.printf("   ");
                }
                for (int j = 0; j <= i; j++) {
                    System.out.printf("%-6d", YangHuiTriangle[i][j]);

                }
                System.out.println();
            }
        }
        if(width == 3) {
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < row-i-1; j++) {
                    System.out.print("\t");
                }
                for (int j = 0; j <= i; j++) {
                    System.out.print(YangHuiTriangle[i][j]+"\t");
                }
                System.out.println();
            }
        }
        if(width != 0 && width != 1 && width != 2 && width != 3)
            System.out.println("杨辉三角参数width，输入有误！！");
        System.out.println();
    }
}
